/**
 * Copyright 2021 Shulie Technology, Co.Ltd
 * Email: shulie@shulie.io
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shulie.instrument.simulator.agent.core.util;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;

import java.io.*;
import java.util.Enumeration;


public class ZipUtils {
    private static final int buffer = 2048;

    public static void writeFile(InputStream in, String dirPath, String fileName, int fileLength, Logger logger) throws IOException {
        File file = new File(dirPath, fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            int size = 0;
            int len = 0;
            byte[] buf = new byte[1024];
            int percent = -1;
            while ((size = in.read(buf)) != -1) {
                len += size;
                out.write(buf, 0, size);
                int newPercent = (int) Math.round((len*1.0 / fileLength) * 100);
                if (percent < newPercent) {
                    logger.info("Downloading file {} -------> {}%", fileName, newPercent);
                    percent = newPercent;
                }
            }
        } finally {
            in.close();
            out.close();
        }

        unZip(file.getAbsolutePath(), file.getParent());
        file.delete();
    }

    /**
     * 解压Zip文件
     *
     * @param path 文件目录
     */
    public static void unZip(String path, String savepath) {
        int count = -1;
        File file = null;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        new File(savepath).mkdir();
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(path, "gbk");
            Enumeration<?> entries = zipFile.getEntries();

            while (entries.hasMoreElements()) {
                byte buf[] = new byte[buffer];

                ZipEntry entry = (ZipEntry) entries.nextElement();

                String filename = entry.getName();
                boolean ismkdir = false;
                if (filename.lastIndexOf("/") != -1) {
                    ismkdir = true;
                }
                filename = savepath + File.separator + filename;

                if (entry.isDirectory()) {
                    file = new File(filename);
                    file.mkdirs();
                    continue;
                }
                file = new File(filename);
                if (!file.exists()) {
                    if (ismkdir) {
                        new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();
                    }
                }
                file.createNewFile();

                is = zipFile.getInputStream(entry);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, buffer);

                while ((count = is.read(buf)) > -1) {
                    bos.write(buf, 0, count);
                }
                bos.flush();
                bos.close();
                fos.close();

                is.close();
            }

            zipFile.close();

        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
                if (zipFile != null) {
                    zipFile.close();
                }
            } catch (Throwable e) {
            }
        }
    }
}
